package com.qqtech.qquan.circle.service;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Sets;
import com.qqtech.core.common.cache.Cache;
import com.qqtech.core.common.util.SettingCfgUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleSysParamDao;
import com.qqtech.qquan.circle.model.CircleSysParam;
import com.qqtech.qquan.circle.model.CircleSysParamVo;

@Service
public class CircleSysParamServiceImpl extends BaseServiceImpl<CircleSysParam>
		implements CircleSysParamService, Cache<String, String> {
	public static final Logger log = LoggerFactory.getLogger(CircleSysParamServiceImpl.class);

	@Resource
	private CircleSysParamDao circleSysParamDao;

	@Override
	protected BaseDao<CircleSysParam> getBaseDao() {
		return circleSysParamDao;
	}

	@Override
	public CircleSysParamVo getByKey(java.lang.String key) {
		return circleSysParamDao.getByKey(key);
	}

	private LoadingCache<String, String> caches = CacheBuilder.newBuilder().recordStats()
			.expireAfterWrite(
					SettingCfgUtil.getConfigHelper("qquan-cfg.properties").getIntValue("paramcache.expire.mins"),
					TimeUnit.MINUTES)
			.build(new CacheLoader<String, String>() {
				public String load(String key) {
					CircleSysParam circleSysParam = circleSysParamDao.getByKey(key);
					if (circleSysParam != null) {
						return circleSysParam.getValue();
					}
					return "";
				}
			});

	@Override
	public String get(String key) {
		return paramValue(key, "");
	}

	public String get(String key, String defaultValue) {
		return paramValue(key, defaultValue);
	}

	public Integer getInt(String key) {
		String v = get(key);
		return Integer.valueOf(v);
	}

	public Integer getInt(String key, int defaultValue) {
		String v = get(key);
		if (StringUtils.isBlank(v)) {
			return defaultValue;
		}
		return Integer.valueOf(v);
	}

	public Float getFloat(String key, float defaultValue) {
		String v = get(key);
		if (StringUtils.isBlank(v)) {
			return defaultValue;
		}
		return Float.valueOf(v);
	}

	public boolean getBoolean(String key, boolean defaultValue) {
		String v = get(key);
		if (StringUtils.isBlank(v)) {
			return defaultValue;
		}
		return Sets.newHashSet("YES", "TRUE", "1", "Y").contains(v.toUpperCase().trim());
	}

	public String paramValue(String key, String defaultValue) {
		String v = caches.getUnchecked(key);
		if (StringUtil.isBlank(v)) {
			return defaultValue;
		}
		return v;
	}

	@Override
	public void clear(String key) {
		caches.invalidate(key);
	}

	@Override
	public void clearAll() {
		caches.invalidateAll();
	}

	@Override
	public int delete(CircleSysParam query) {
		if (query != null && StringUtil.isNotBlank(query.getKey())) {
			clear(query.getKey());
		}
		return super.delete(query);
	}

	@Override
	public int deleteAll() {
		clearAll();
		return super.deleteAll();
	}

	@Override
	public int deleteById(Integer id) {
		CircleSysParam circleSysParam = getById(id);
		if (circleSysParam != null && StringUtil.isNotBlank(circleSysParam.getKey())) {
			clear(circleSysParam.getKey());
		}
		return super.deleteById(id);
	}

	@Override
	public int insert(CircleSysParam entity) {
		if (entity != null && StringUtil.isNotBlank(entity.getKey())) {
			clear(entity.getKey());
		}
		return super.insert(entity);
	}

	@Override
	public int updateById(CircleSysParam entity) {
		if (entity != null && StringUtil.isNotBlank(entity.getKey())) {
			clear(entity.getKey());
		}
		return super.updateById(entity);
	}
}